package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Warn;
import com.ruoyi.system.domain.vo.WarnCount;
import com.ruoyi.system.mapper.WarnMapper;
import com.ruoyi.system.service.IWarnService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 报警Controller
 *
 * @author wxd
 * @date 2024-08-31
 */
@Api(value = "报警数据", tags = {"报警数据"})
@RestController
@RequestMapping("/system/warn")
public class WarnController extends BaseController {
    @Autowired
    private IWarnService warnService;
    @Autowired
    private WarnMapper warnMapper;

    /**
     * 查询报警列表
     */
    @ApiOperation("查询分页列表")
    @GetMapping("/list")
    public TableDataInfo list(Warn warn) {
        startPage();
        List<Warn> list = warnService.selectWarnList(warn);
        TableDataInfo dataTable = getDataTable(list);
        WarnCount warnCount = warnMapper.warnCountPage(warn);
        dataTable.setCountData(warnCount);
        return dataTable;
    }
    /**
     * 查询报警列表
     */
    @ApiOperation("报警统计")
    @GetMapping("/reportWarn")
    public TableDataInfo reportWarn(Warn warn) {
        startPage();
        List<WarnCount> list = warnMapper.reportWarn(warn);
        TableDataInfo dataTable = getDataTable(list);
        WarnCount warnCount = warnMapper.warnCountPage(warn);
        dataTable.setCountData(warnCount);
        return dataTable;
    }

    @ApiOperation("查询字段分组列表")
    @Anonymous
    @GetMapping("/fieldGroup")
    public List<String> fieldGroup(String fieldName) {
        return warnMapper.selectFieldGroup(fieldName);
    }

    /**
     * 导出报警列表
     */
    @ApiOperation("导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Warn warn) {
        List<Warn> list = warnService.selectWarnList(warn);
        ExcelUtil<Warn> util = new ExcelUtil<Warn>(Warn.class);
        util.exportExcel(response, list, "报警数据");
    }

    /**
     * 获取报警详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(warnService.selectWarnById(id));
    }

    /**
     * 新增报警
     */
    @ApiOperation("新增")
    @Anonymous
    @PostMapping
    public AjaxResult add(@RequestBody Warn warn) {
        warn.setId(null);
        return toAjax(warnService.insertWarn(warn));
    }

    /**
     * 修改报警
     */
    @ApiOperation("修改")
    @PutMapping
    public AjaxResult edit(@RequestBody Warn warn) {
        return toAjax(warnService.updateWarn(warn));
    }

    /**
     * 删除报警
     */
    @ApiOperation("删除")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(warnService.deleteWarnByIds(ids));
    }

    /**
     * 删除报警
     */
    @ApiOperation("清空")
    @DeleteMapping("/clear")
    public AjaxResult clear() {
        warnService.clear();
        return success();
    }

    /**
     * 删除报警
     */
    @ApiOperation("批量处理报警")
    @PostMapping("/handle/{ids}")
    public AjaxResult handle(@PathVariable Long[] ids) {
        return toAjax(warnMapper.handle(ids));
    }
}
